/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sidora.strata;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CallableSystemAction;
import org.sidora.core.context.Context;

public final class ReportAction extends CallableSystemAction {

    public void performAction() {
        
   List contexts = Context.Inventory.getList();

   // pass parameters to the report
    Map parameters = new HashMap();
    parameters.put("Title", "ContextReport");

    try {
      // compile report design
      JasperReport jasperReport = JasperCompileManager
          .compileReport("ContextReport.jrxml");
      
      // create an object datasourse from the pilots list
      ObjectDataSource dataSource = new ObjectDataSource(contexts);

      // fill the report 
      JasperPrint jasperPrint = JasperFillManager.fillReport(
          jasperReport, parameters, dataSource);
      
      // export result to the *.pdf
      JasperExportManager.exportReportToPdfFile(jasperPrint,
          "ContextReport.pdf");
      
      // or export to *.html
      JasperExportManager.exportReportToHtmlFile(jasperPrint,
      "ContextReport.html");

      // or view it immediately in the Jasper Viewer
      JasperViewer.viewReport(jasperPrint);
    } catch (JRException e) {
      e.printStackTrace();
    }
    }

    public String getName() {
        return NbBundle.getMessage(ReportAction.class, "CTL_ReportAction");
    }

    @Override
    protected String iconResource() {
        return "org/sidora/resources/report32.png";
    }

    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
    }

    @Override
    protected boolean asynchronous() {
        return false;
    }
}
